<?php
/**
 * Main class for module iChat
 * 
 * @package      WebGuru3
 * @subpackage   modules/ichat/
 * @author       Ondrej Rafaj
 * @author       WebGuruCMS3 Framework CMS admin generator (http://www.webgurucms.com)
 * @version      1.0.0.0
 * @wgversion    3.0.0.0
 * @wgdeveloper  1.0.0.1
 * @since        5. February 2010
 */

class moduleIchat {
	
	public $name            = NULL;
	public $version         = NULL;
	public $author          = NULL;
	
	private static $_path   = NULL;
	private static $_module = NULL;
	
	
	public function __construct() {
		$this->_init();
	}
	
	
	private function _init() {
		$this->name    = 'iChat';
		$this->code    = 'ichat';
		$this->version = '0.0.0.1';
		$this->author  = 'Ondrej Rafaj';
		
		$this->_module = dbSystem::getModulesByName($this->code);
		$this->_path   = wgPaths::getAdminPath().$this->_module['part'].'/';
		
		
	}
	
	// ------------------------- class functions -------------------------
	
	public function sendMessage($apiCode, $data) {
		$ret = array();
		$apiCode = wgText::safeText($apiCode);
		$ret['result'] = 0;
		$ret['error'] = '';
		if (empty($apiCode)) {
			$ret['error'] = 'NO_API_CODE';
			return $ret;
		}
		$group = IchatGroupsModel::getOneCodeData($apiCode);
		if (!(bool) $group->getId()) {
			$ret['error'] = 'WRONG_API_CODE';
			return $ret;
		}
		else $id = $group->getId();
		$save = array();
		$save['ichat_groups_id'] = $id;
		$save['added'] = 'NOW()';
		$save['nickname'] = wgPost::getValue('nickname');
		$save['message'] = wgPost::getValue('message');
		$save['devid'] = wgText::safeText(wgPost::getValue('devid'));
		$save['longitude'] = wgPost::getValue('longitude');
		$save['latitude'] = wgPost::getValue('latitude');
		$save['altitude'] = (int) wgPost::getValue('altitude');
		$save['accurancy'] = (int) wgPost::getValue('accurancy');
		$ok = (bool) IchatMessagesModel::doInsert($save);
		$ret['result'] = (int) $ok;
		if (!$ok) $ret['error'] = 'CANT_SAVE_TO_DB';
		return $ret;
	}
}
		
?>